﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.membercenter.order
{
    public partial class order_info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            YXShop.BLL.Order.Orders orderBll = new YXShop.BLL.Order.Orders();
            if (!IsPostBack)
            {
                int id = ChangeHope.WebPage.PageRequest.GetQueryInt("OrderId");
                if (id > 0)
                {
                    InitWebControl(id);
                    OrderInfo(id);
                    OrdersProduct(id);
                }
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != "")
                {
                    YXShop.BLL.Member.MemberAccount memberBll = new YXShop.BLL.Member.MemberAccount();
                    string types = Request["Option"].Trim();
                    string orderId = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    /**
                     *kevin
                     *修改时间：09.12.26
                     * 
                     */
                    if (types == "del")
                    {
                        YXShop.Model.Order.Orders orderModel = orderBll.GetModel(Convert.ToInt32(orderId));
                        if (orderModel.OrderStatus == 7)
                        {
                            orderBll.Delete(orderId);
                        }
                        else
                        {
                            Response.Write("对不起，您不能进行此操作！");
                        }
                        
                    }
                    Response.End();
                    return;
                }

                #region 订单操作
                string option = ChangeHope.WebPage.PageRequest.GetQueryString("option");
                if (option != string.Empty && id > 0)
                {
                    YXShop.Model.Order.Orders orderModel = orderBll.GetModel(id);
                    if (option == "cusover")
                    {
                        if (orderModel.OgisticsStatus == 1)
                        {
                            int Status = YXShop.Common.OrdersStatusEnum.OgisticsStatus.已签收.GetHashCode();
                            ModfiyOrderStatus(id, "OgisticsStatus", Status, "操作成功，订单物品已经签收。");
                        }
                        else
                        {
                            ChangeHope.WebPage.Script.AlertAndGoBack("货物还没发送，您还不能进行此操作！");
                        }
                        
                    }
                }
                #endregion
            }

        }

        #region 订单操作
        private void InitWebControl(int id)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            YXShop.BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
            YXShop.Model.Order.Orders model = bll.GetModel(id);
            int orderStateId = 0;
            if (model != null)
            {
                orderStateId =Convert.ToInt32(model.OrderStatus);
            }
            string payUrl = YXShop.TemplateAction.Common.UrlStr("paymentorders", "OrderId=" + model.OrderId + "", "product/");
            if (model.PaymentStatus != 1)
            {
                this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"order_payment.aspx?id=" + id + "\">预付款支付</a></li>";
                this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"" + payUrl + "\">支付</a></li>";
            }
            this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"order_feedback.aspx?id=" + id + "\">添加反馈</a></li>";
            this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"order_hadpay.aspx?id=" + id + "\">我已汇款</a></li>";
            this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"#\" onclick=\"Del(" + id + ")\">删除订单</a></li>";
            this.linklist.Text += "<li><img src=\"../../admin/images/mune.gif\" alt=\"\"/>&nbsp;<a href=\"order_info.aspx?OrderId=" + id + "&option=cusover\">签收物品</a></li>";
        }
        #endregion

        #region 订单信息
        private void OrderInfo(int id) 
        {
            YXShop.BLL.Order.Orders bll = new YXShop.BLL.Order.Orders();
            YXShop.Model.Order.Orders model = bll.GetModel(id);
            if (model != null)
            {
                YXShop.BLL.Member.MemberAccount mabll = new YXShop.BLL.Member.MemberAccount();
                YXShop.Model.Member.MemberAccount mamodel = mabll.GetModel(model.UserId);
                if (mamodel != null)
                {
                    this.lbUserName.Text = "<a href='../individualinfo/memberinfo_show.aspx?uid=" + mamodel.UID + "'>" + model.UserId + "</a>";
                }
                else
                {
                    this.lbUserName.Text = model.UserId;
                }
                this.blOrderNo.Text = model.OrderId;
                this.lbName.Text = model.ConsigneeName;
                YXShop.Common.OrdersStatusEnum ose = new YXShop.Common.OrdersStatusEnum();
                this.lbOrderStatue.Text = ose.OrderStatus(model.OrderStatus);
                this.lbLogisticsStatus.Text = ose.OgisticsStatu(model.OgisticsStatus);
                this.lbPayment.Text = ose.PaymentStatus(model.PaymentStatus);
                this.lbOrderDateTime.Text = model.OrderDate.ToString();
                this.lbConsigneeName.Text = model.ConsigneeName;
                this.lbConsigneeAddress.Text = model.ConsigneeAddress;
                this.lbConsigneeEmail.Text = model.ConsigneeEmail;
                this.lbConsigneeModile.Text = model.ConsigneePhone;
                this.lbConsigneeTel.Text = model.ConsigneeTel;
                this.lbConsigneeZip.Text = model.ConsigneeZip;
                this.lbPaymentMode.Text = Payments(model.PaymentType);
                
                this.lbDeliveryMode.Text = this.Courier(int.Parse(model.Carriage.ToString()));
                this.lbConstructionSigns.Text = model.ConsigneeRealName;
                this.lbConsigneTime.Text = model.ConsigneeFax;
                this.lbOrderTotalPrice.Text = Convert.ToDouble(model.TotalPrice).ToString("f2");
                this.lbCarriage.Text = Convert.ToDouble(model.Courier).ToString("f2");
                this.lblMoney.Text = Convert.ToDouble(model.FactPrice).ToString("f2");
                this.lblEndMoney.Text = Endpayment(model.OrderId)+"元";
            }

        }
        #endregion

        #region 配送信息
        private string Courier(int id)
        {
            string reStr = string.Empty;
            YXShop.BLL.SystemInfo.PostArea bll = new YXShop.BLL.SystemInfo.PostArea();
            YXShop.Model.SystemInfo.PostArea model = bll.GetModelByAreaID(id);
            if (model != null)
            {
                if (model.Deliverymode > 0)
                {
                    YXShop.BLL.SystemInfo.Deliver dbll = new YXShop.BLL.SystemInfo.Deliver();
                    YXShop.Model.SystemInfo.Deliver dmodel = dbll.GetModelByID(model.Deliverymode);
                    if (dmodel != null)
                    {
                        reStr = dmodel.Distributionname;
                    }
                }
            }
            return reStr;
        }
        #endregion

        #region 计算商品总价
        private double TotalPrice(string orderId)
        {
            YXShop.BLL.Order.OrderProduct bll = new YXShop.BLL.Order.OrderProduct();
            DataTable dt = bll.GetListOrderProduct(orderId);
            double totalPrice = 0;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    totalPrice += Convert.ToDouble(dt.Rows[i]["ProPrice"].ToString()) * Convert.ToDouble(dt.Rows[i]["ProNum"].ToString());
                }
            }
            return totalPrice;
        }
        #endregion


        #region 商品信息
        private void OrdersProduct(int id)
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Order.OrderProduct bll = new YXShop.BLL.Order.OrderProduct();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetListByPage(" and OrderId="+id);
            table.AddHeadCol("", "商品名称");
            table.AddHeadCol("", "单价");
            table.AddHeadCol("", "数量");
            table.AddHeadCol("", "小计(单位：元)");
            table.AddRow();
            double ProductTotalPrice = 0;
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    string fittingsProductId = dataPage.DataReader["FittingsProductId"].ToString();
                    string fittingsId = dataPage.DataReader["FittingsId"].ToString();
                    string fittingsProductCount = dataPage.DataReader["FittingsProductCount"].ToString();
                    string fittingProductInfo = "";
                    double fittingTotalPrice = 0;
                    if (!string.IsNullOrEmpty(fittingsProductId.Trim()) && !string.IsNullOrEmpty(fittingsId.Trim()) && Convert.ToInt32(fittingsId) > 0)
                    {
                        //配件
                        int fittingProductCount = 0;
                        double fittingProductWeight = 0;
                        double fittingProductIntergal = 0;
                        YXShop.Common.ProductInfo.FittingDisposal(fittingsId, fittingsProductId, fittingsProductCount, out fittingProductCount, out fittingProductInfo, out fittingTotalPrice, out fittingProductWeight, out fittingProductIntergal);
                    }
                    string specificationValue = dataPage.DataReader["Specification"].ToString();
                    string specVa = "";
                    if (specificationValue != "")
                    {
                        specVa = "&nbsp;&nbsp;" + specificationValue + "";
                    }

                    table.AddCol("<a href='../../product/productcontent.aspx?q_productid=" + dataPage.DataReader["ProId"].ToString() + "' target='_blank'>" + dataPage.DataReader["ProName"].ToString()  +specVa + fittingProductInfo + "</a>");
                    table.AddCol(Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()).ToString("f4"));
                    table.AddCol(dataPage.DataReader["ProNum"].ToString());
                    table.AddCol((Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()) * Convert.ToDouble(dataPage.DataReader["ProNum"].ToString())).ToString("f4"));
                    table.AddRow();
                    ProductTotalPrice += Convert.ToDouble(dataPage.DataReader["ProPrice"].ToString()) * Convert.ToDouble(dataPage.DataReader["ProNum"].ToString()) + fittingTotalPrice;
                }
                table.AddCol("4", "<strong>本页金额合计：" + ProductTotalPrice.ToString("f4") + "</strong>");
                table.AddRow();
                string view = table.GetTable() + dataPage.PageToolBar;
                dataPage.Dispose();
                dataPage = null;
                this.litData.Text = view;
            }
            else
            {
                this.litData.Text = "没有找到该订单的商品信息";
            }
        }   
        #endregion

        #region 已付款
        public string Endpayment(string orderId)
        {
            string totle = string.Empty;
            decimal remPrice = 0;
            decimal payPrice = 0;
            decimal prePrice = 0;
            //银行支付
            YXShop.BLL.Order.RemittanceInfo remBll = new YXShop.BLL.Order.RemittanceInfo();
            List<YXShop.Model.Order.RemittanceInfo> remdate = remBll.GetAll("orderid='" + orderId + "'");
            if (remdate.Count > 0)
            {
                foreach (YXShop.Model.Order.RemittanceInfo da in remdate)
                {
                    remPrice += Convert.ToDecimal(da.RemittanceMoney.ToString());
                }
            }
            //现金支付
            YXShop.BLL.Order.PaymentMoney payBll = new YXShop.BLL.Order.PaymentMoney();
            List<YXShop.Model.Order.PaymentMoney> paydata = payBll.GetAll("orderid='" + orderId + "'");
            if (paydata.Count > 0)
            {
                foreach (YXShop.Model.Order.PaymentMoney da in paydata)
                {
                    payPrice += Convert.ToDecimal(da.GatheringMoney.ToString());
                }
            }
            //预付款
            YXShop.BLL.Order.PrepayMoney preBll = new YXShop.BLL.Order.PrepayMoney();
            List<YXShop.Model.Order.PrepayMoney> predata = preBll.GetAll("orderid='" + orderId + "'");
            if (predata.Count > 0)
            {
                foreach (YXShop.Model.Order.PrepayMoney da in predata)
                {
                    prePrice += Convert.ToDecimal(da.PayoutMoney);
                }
            }
            totle = (remPrice + payPrice + prePrice).ToString();
            return totle;
        }
        #endregion

        private void ModfiyOrderStatus(int id, string ColumnName, int Value, string Str)
        {
            YXShop.BLL.Order.Orders ordbll = new YXShop.BLL.Order.Orders();
            ordbll.Amend(id, ColumnName, Value);
            ChangeHope.WebPage.Script.AlertAndRedirect(Str, "order_info.aspx?OrderId=" + id + "");
        }

        #region 付款方式
        private string Payments(string PaymentId)
        {
            if (!string.IsNullOrEmpty(PaymentId))
            {
                YXShop.BLL.SystemInfo.TerraceManage tmbll = new YXShop.BLL.SystemInfo.TerraceManage();
                YXShop.Model.SystemInfo.TerraceManage tmmodel = tmbll.GetModelById(Convert.ToInt32(PaymentId));
                if (tmmodel != null)
                {
                    return tmmodel.Tmname;
                }
            }
            return string.Empty;
        }
        #endregion
    }

       
}
